package ihm;

import javax.swing.JFrame;
import javax.swing.JList;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import javax.swing.ListSelectionModel;

public class FenPrincipal {
	
	private JFrame frame;
	private JList lstType;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FenPrincipal window = new FenPrincipal();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FenPrincipal() {
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();

		frame.setBounds(400, 300, 600, 400);

		frame.setBounds(100, 100, 609, 389);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		DefaultListModel listModel = new DefaultListModel();
		
		lstType = new JList();
		lstType.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		lstType.setCellRenderer(new DefaultListCellRenderer());
		lstType.setModel(listModel);
		lstType.setSelectedIndex(0);
		listModel.addElement("Collaborateur");
		listModel.addElement("Contrat");
		listModel.addElement("Tache");
		
		JLabel lblType = new JLabel("Type");
		
		
		JList lstDetails = new JList();
		lstDetails.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstDetails.setCellRenderer(new DefaultListCellRenderer());
		lstDetails.setModel(listModel);
		
		if (lstType.isSelectedIndex(0)) {
			listModel.addElement("CACA"); 
			}
			
		else if  (lstType.isSelectedIndex(1)) {
			listModel.addElement("PIPI"); 
			}
		
		else if (lstType.isSelectedIndex(2)){
			listModel.addElement("LEA"); 
			}
		
		else {}
		
		JButton btnQuitter = new JButton("Quitter");
		btnQuitter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		
		JButton btnAjouter = new JButton("Ajouter");

		
		
		btnAjouter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
			if (lstType.getSelectedIndex() == 0){ //fenetre collaborateur 
				FrmAjouterCollaborateur fenetreCollab = new FrmAjouterCollaborateur();
				fenetreCollab.setVisible(true);
				fenetreCollab.setBounds(400, 300, 600, 400);
			}
			else if (lstType.getSelectedIndex() == 1){ //fenetre contrat
				
				FrmAjouterContrat fenetreContrat = new FrmAjouterContrat();
				fenetreContrat.setVisible(true);
				fenetreContrat.setBounds(400, 300, 600, 400);
			}
			else if (lstType.getSelectedIndex() == 2){ //fenetre collaborateur 
				
				FrmAjouterTache fenetreTache = new FrmAjouterTache();
				fenetreTache.setVisible(true);
				fenetreTache.setBounds(400, 300, 600, 400);
				
			}
			}
		});
		
		JButton btnModifier = new JButton("Modifier");
		
		JButton btnSupprimer = new JButton("Supprimer");
		
		JLabel lblDtails = new JLabel("Détails");
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(235)
							.addComponent(btnQuitter))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lstType, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblType))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblDtails)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lstDetails, GroupLayout.PREFERRED_SIZE, 311, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(btnSupprimer, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(btnModifier, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(btnAjouter, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(32)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblType)
								.addComponent(lblDtails))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lstType, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
								.addComponent(lstDetails, GroupLayout.PREFERRED_SIZE, 263, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
							.addComponent(btnQuitter))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(92)
							.addComponent(btnAjouter)
							.addGap(18)
							.addComponent(btnModifier)
							.addGap(18)
							.addComponent(btnSupprimer)))
					.addContainerGap())
		);
		frame.getContentPane().setLayout(groupLayout);
	}
	
}